package com.cn.core.utils;



import java.io.*;

public class FileUtils {

    /**
     * 读取本地类路径文件
     *
     * @param path 路径
     * @return {@link String }
     */
    public static String readLocalClassPathFile(String path){
        StringBuilder stringBuilder = new StringBuilder();
        InputStream inputStream = FileUtils.class.getClassLoader().getResourceAsStream(path);
        if (inputStream != null) {
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return stringBuilder.toString();
    }
    /**
     * 读取文件行文本
     * 方法简介:
     * 业务背景:
     *
     * @param filePath 文件路径
     * @return {@link String }
     **/
    public static String readFileLineText(String filePath) {
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath))) {
            String line;
            StringBuilder stringBuilder = new StringBuilder();
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line).append("/n");
            }
            return stringBuilder.toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void writer(String filePath, String content) throws IOException {
        try (PrintWriter writer = new PrintWriter(new FileWriter(filePath, true))) {
            writer.println(content); // Adds a new line at the end
        }
    }

    public static void main(String[] args) {
        String s = readFileLineText("C:\\Users\\Administrator\\Desktop\\address.txt");
        String[] split = s.split("/n");
        for (String string : split) {
            System.out.println(string);
        }

    }

}